<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>用户管理</title>
	<link rel="stylesheet" href="/static/admin/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
	<div class="layui-card-body">
		<table id="user-table" lay-filter="user-table"></table>
	</div>
</div>
<script type="text/html" id="user-toolbar">
	<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		<i class="layui-icon layui-icon-add-1"></i>
		新增
	</button>
</script>

<script type="text/html" id="user-bar">
	<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
	<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="user-enable">
	<input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" checked = "{{ d.enable == 0 ? 'true' : 'false' }}">
</script>

<script type="text/html" id="user-createTime">
	{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>

<script src="/static/admin/component/layui/layui.js"></script>
<script src="/static/admin/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        table.render({
            elem: '#user-table',
            url: '{:url("admin/user/queryUser")}',
            page: true,
            cols: [[
                {field: 'user_name', title: '用户名',width:200}
                ,{field: 'real_name', title: '用户名',width:200}
                ,{field: 'create_time', title: '注册时间', width: 200}
                ,{field: 'last_login_time', title: '最后登入时间', width: 200}
                ,{field: 'image', title: '图片', width: 200}
                ,{field: 'status',title: '状态', align: 'center', templet: '#user-enable', width: 100}
            ]],
            text: {
                none : '暂无相关数据'
            },
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [ 'filter']
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });


        form.on('switch(user-enable)', function(obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        window.add = function() {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile()?'100%':'100%', common.isModile()?'100%':'100%'],
                content:'{:url("/index/Banner/addBanner")}'
            });
        }

        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: [common.isModile()?'100%':'100%', common.isModile()?'100%':'100%'],
                content:'{:url("/index/Banner/editBanner")}?id='+obj.data['id']
            });
        }

        window.remove = function(obj) {
            layer.confirm('确定要删除该banner？', {
                icon: 3,
                title: '提示'
            }, function() {
                $.ajax({
                    url:  '{:url("/index/Banner/delete_banner")}',
                    dataType: 'json',
                    data:{
                      id : obj.data['id']
                    },
                    type: 'post',
                    success (res) {
                        if (res.code == 1) {
                            layer.msg(res.msg, {
                                icon: 6,
                                time: 1000
                            });
                            window.location.href = 'banner.html';
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>